"""

I think I need some way of keeping my iphone code inline with my ipad code.

This my plan:

1. Create ipad project and fill in code to do the work.
2. Create companion iphone project but do not fill in any code.

NOTE: projects must be called ipad-<app> and iphone-<app> and for must be in the same folder.
NOTE: h files MUST match exactly take care to create the same names in the same order in the user interafce

3. Do all edits to ipad project and keep iphone project in step by using this script:

$ python ipad2iphone.py objectivec/ipad-app1 [-test (default)] [-update]

NOTE: --test is the default and simply lists differences found
NOTE: --update changes .m files
NOTE: m files that differ are copied from the ipad version and everything ipad is renamed to iphone
NOTE: old m files are saved as <file>.m-saved-date-time

"""

from sys import argv
from os import environ, rename, path
from glob import glob
from datetime import datetime
from difflib import Differ
differ = Differ()

doit = False
if '-update' in argv:
    doit = True
    argv.remove('-update')

if '-test' in argv:
    doit = False
    argv.remove('-test')

assert len(argv)>1, 'need path to project e.g ~/source/ipad-app1'
ipad_project = argv[1]
if ipad_project.endswith('/'):
    ipad_project = ipad_project[:-1]

assert path.isdir(ipad_project)

if '/' in ipad_project:
    root, ipad_project=ipad_project.rsplit('/',1)
else:
    root = '.'

assert 'ipad-' in ipad_project, 'project name must start "ipad-"'
found_ipad_project = glob('%s' % ipad_project)
assert len(found_ipad_project)==1, found_ipad_project
found_ipad_project = found_ipad_project[0]

iphone_project = ipad_project.replace('ipad', 'iphone')
found_iphone_project = glob('%s' % iphone_project)
assert len(found_iphone_project)==1, found_iphone_project
found_iphone_project = found_iphone_project[0]

def filter(project, ext):
    return [_ for _ in glob ('%s/%s/%s/*' % (root,project, project)) if _.endswith(ext)]

ipad_h_files = filter(ipad_project, '.h')
ipad_m_files = filter(ipad_project, '.m')
iphone_h_files = filter(iphone_project, '.h')
iphone_m_files = filter(iphone_project, '.m')

def p2p(string):
    if 'ipad' in string:
        assert 'iphone' not in string
        return string.replace('ipad', 'iphone')
    elif 'iphone' in string:
        return string.replace('iphone', 'ipad')
    else:
        return string

def changep2p(f1):
    if not doit:
        return
    ext = str(datetime.now()).split('.')[0].replace(':', '-').replace('-', '').replace(' ','-')
    print 'archive', p2p(f1), ext
    rename (p2p(f1), '%s-%s' % (p2p(f1), ext)) 
    lines = [p2p(_) for _ in open(f1).readlines()]
    open (p2p(f1), 'w').writelines(lines)

def comparep2p(f1):
    lines1 = open(f1).readlines()
    lines2 = [p2p(_) for _ in open(p2p(f1)).readlines()]
    result = ''.join([_ for _ in list(differ.compare(lines1, lines2)) if _.strip() and not _.startswith(' ')])
    if not result:
        return
    print f1
    print result
    changep2p(f1)

for h in iphone_h_files:
    assert p2p(h) in ipad_h_files
for h in ipad_h_files:
    assert p2p(h) in iphone_h_files
    comparep2p(h)

for m in iphone_m_files:
    assert p2p(m) in ipad_m_files
for m in ipad_m_files:
    assert p2p(m) in iphone_m_files
    comparep2p(m)
